package com.xmkj.jijidriver.count;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.common.cons.Constans;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.methods.SetFindPswMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.utils.EmptyUtils;
import com.common.utils.SPUtils;
import com.xmkj.jijidriver.R;

/**
 * Created by chen on 2018/3/26.
 */

public class ModifyPswActivity extends BaseMvpActivity {
    public static final String SMSCODE = "SMSCODE";
    public static final String MOBILE = "MOBILE";
    public static final String UID = "c";
    private String mCode;
    private String mMobile;
    private String mMUid = "";
    EditText et_psw ;
    EditText et_psw_again ;
    TextView tv_finish  ;
    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_modify_psw;
    }


    @Override
    protected void onViewCreated() {
        mCode = getIntent().getStringExtra(SMSCODE);
        mMobile = getIntent().getStringExtra(MOBILE);

        et_psw = findViewById(R.id.et_psw);
        et_psw_again = findViewById(R.id.et_psw_again);
        tv_finish = findViewById(R.id.tv_finish);
    }

    @Override
    protected void doLogicFunc() {
        attachClickListener(tv_finish);
    }

    @Override
    protected void onViewClicked(View view) {
        if(view.getId()==tv_finish.getId()){
            if(EmptyUtils.isEmpty(getEditTextStr(et_psw))||EmptyUtils.isEmpty(getEditTextStr(et_psw_again))){
                showToastMsg("请输入密码");
                return;
            }else if(!getEditTextStr(et_psw).equals(getEditTextStr(et_psw_again))){
                showToastMsg("两次密码输入不一样");
                return;
            }
            gotoModify();
        }
    }

    private void gotoModify() {


        showProgressingDialog();
        CommonSubscriber<Object> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                onBackPressed();
                SPUtils.setShareInt(Constans.UID, Integer.MAX_VALUE);
                SPUtils.setShareString(Constans.HASHID, "");
                SPUtils.setShareString(Constans.USERNAME, "");
                SPUtils.setShareString(Constans.MOBILE, "");
                SPUtils.setShareString(Constans.NAME, "");
                SPUtils.setShareString(Constans.FACADE, "");
                SPUtils.setShareString(Constans.EMAIL, "");
                SPUtils.setShareInt(Constans.FIRST, Integer.MAX_VALUE);
                gotoActivity(LoginActivtiy.class);
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });

        SetFindPswMethods.getInstance().setPsw(subscriber,mMobile,mCode,getEditTextStr(et_psw),getEditTextStr(et_psw_again));
        rxManager.add(subscriber);
    }

    @Override
    protected void setNavigation() {
        setNavigationBack("修改密码");
    }
}
